using System;
using System.Drawing;
using System.ComponentModel;
using System.Windows.Forms;

namespace asWins.Forms
{
	/// <summary>
	/// Summary description for ButtonMini.
	/// </summary>
	public class ButtonMini : System.Windows.Forms.Button
	{
		public ButtonMini()
		{
			SetStyle(ControlStyles.Selectable
				| ControlStyles.StandardDoubleClick
				, false);

			SetStyle(ControlStyles.AllPaintingInWmPaint
				| ControlStyles.DoubleBuffer
				| ControlStyles.ResizeRedraw
				| ControlStyles.UserPaint
				, true);
			this.FlatStyle = FlatStyle.Popup;
			this.m_ImageTransparent = Color.Empty;
			this.m_toolTip = null;
			this.components = new Container();
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
				if (components != null)
					components.Dispose();
			}
			base.Dispose(disposing);
		}


		private enum ButtonFlags
		{
			None,
			Press,
			Hover
		}


		/// <summary>
		/// State of button
		/// </summary>
		private ButtonMini.ButtonFlags ButtonFlag = ButtonFlags.None;

		/// <summary>
		/// Component for this button.
		/// </summary>
		private IContainer components;

		/// <summary>
		/// Tooltip
		/// </summary>
		private ToolTip m_toolTip;


		private void MakeImageTransparent(Color transparentColor)
		{
			Image backImg = this.Image;
			if (backImg != null)
			{
				Bitmap bmp = new Bitmap(backImg);
				bmp.MakeTransparent(transparentColor);
				this.Image = bmp;
			}
		}


		protected override void OnMouseDown(MouseEventArgs e)
		{
			this.ButtonFlag = ButtonFlags.Press;
			Invalidate();

			base.OnMouseDown (e);
		}

		protected override void OnMouseUp(MouseEventArgs e)
		{
			this.ButtonFlag = ButtonFlags.Hover;
			Invalidate();
			
			base.OnMouseUp (e);
		}

		protected override void OnMouseHover(EventArgs e)
		{
			this.ButtonFlag = ButtonFlags.Hover;
			Invalidate();

			base.OnMouseHover (e);
		}

		protected override void OnMouseLeave(EventArgs e)
		{
			if (this.ButtonFlag != ButtonFlags.None)
			{
				this.ButtonFlag = ButtonFlags.None;
				Invalidate();
			}
			base.OnMouseLeave (e);
		}

		protected override void OnPaint(PaintEventArgs e)
		{
			base.OnPaint (e);
			this.DrawBorder(e.Graphics);
		}

		private void DrawBorder(Graphics g)
		{
			ButtonBorderStyle bs = ButtonBorderStyle.None;
			int borderWidth = 1;
			Color color_LeftTop = this.BackColor;
			Color color_RightBottom = this.BackColor;

			switch (this.ButtonFlag)
			{
				case (ButtonFlags.None):
					bs = ButtonBorderStyle.Solid;
					color_LeftTop = this.BackColor;
					color_RightBottom = this.BackColor;
					break;
				case (ButtonFlags.Hover):
					bs = ButtonBorderStyle.Solid;
					color_LeftTop = Color.Black;
					color_RightBottom = Color.Black;
					break;
				case (ButtonFlags.Press):
					bs = ButtonBorderStyle.Inset;
					color_LeftTop = this.BackColor;
					color_RightBottom = this.BackColor;
					break;
			}

			ControlPaint.DrawBorder(g, this.ClientRectangle, 
				color_LeftTop, borderWidth, bs,
				color_LeftTop, borderWidth, bs,
				color_RightBottom, borderWidth, bs,
				color_RightBottom, borderWidth, bs);
		}

		private Color m_ImageTransparent;
		/// <summary>
		/// Get or set background image transparent.
		/// </summary>
		[
		Description("Get or set background image transparent."),
		Category("Appearance"),
		Browsable(true),
		DefaultValue(typeof(Color), "Empty")
		]
		public Color ImageTransparent
		{
			get {return m_ImageTransparent;}
			set 
			{
				if (m_ImageTransparent != value)
				{
					m_ImageTransparent = value;
					this.MakeImageTransparent(value);
				}
			}
		}

		private string m_ToolTipText;
		/// <summary>
		/// Get or set tooltip text for short description.
		/// </summary>
		[
		Description("Get or set tooltip text for short description."),
		Category("Appearance"),
		Browsable(true),
		DefaultValue("")
		]
		public string ToolTipText
		{
			get {return m_ToolTipText;}
			set 
			{
				if (m_ToolTipText != value)
				{
					if (m_toolTip == null)
						m_toolTip = new ToolTip(this.components);
					m_ToolTipText = value;
					m_toolTip.SetToolTip(this, value);
				}
			}
		}



		protected override Size DefaultSize
		{
			get
			{
				return new Size(15, 15);
			}
		}


	}
}
